package com.pda.xinma.ui.manmade;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinma.R;
import com.pda.xinma.adapter.EndFaceDetailAdapter;
import com.pda.xinma.bean.ArtificialListBean;
import com.pda.xinma.bean.EndFaceDetailBean;
import com.pda.xinma.bean.EndFaceListBean;
import com.pda.xinma.bean.LatheMachinBean;
import com.pda.xinma.bean.ProcessBean;
import com.pda.xinma.bean.SelectBean;
import com.pda.xinma.bean.request.ManMadeRequest;
import com.pda.xinma.contract.EndFaceDetailContract;
import com.pda.xinma.dialog.AffirmDialog;
import com.pda.xinma.presenter.EndFaceDetailPresenter;
import com.pda.xinma.utils.PopUtisl;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.ENDFACEDETAILACTIVITY)
public class EndFaceDetailActivity extends BaseMvpActivity<EndFaceDetailPresenter> implements EndFaceDetailContract.View {
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_connectionCode)
    TextView tvConnectionCode;
    @BindView(R.id.tv_machine)
    TextView tvMachine;
    @BindView(R.id.tv_processId)
    TextView tvProcessId;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.sb_scan)
    TextView sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;
    EndFaceDetailAdapter endFaceDetailAdapter;
    ArtificialListBean.DeliveryVosBean endFaceListBean;
    SelectBean processBean;
    @BindView(R.id.tv_orderNo)
    TextView tvOrderNo;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_detail)
    TextView tvDetail;

    @Override
    public EndFaceDetailPresenter getPresenter() {
        return new EndFaceDetailPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_man_face_detail;
    }

    @Override
    public void initView() {
        endFaceDetailAdapter = new EndFaceDetailAdapter(R.layout.item_man_face_detail, new ArrayList<>());
        rvList.setAdapter(endFaceDetailAdapter);
        endFaceListBean = (ArtificialListBean.DeliveryVosBean) getIntent().getExtras().getSerializable(StringKey.QQ);
        tvStorageBin.setText(endFaceListBean.getStorageBinId());
        tvOrderNo.setText(endFaceListBean.getProOrderNo());
        presenter.selectDeliveryDetail(endFaceListBean.getProOrderNo());
        //tvConnectionCode.setText(endFaceListBean.get);
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }

    String lathCode;

    @Override
    public void setLatheList(List<LatheMachinBean> list) {
        List<SelectBean> selectBeans = new ArrayList<>();
        for (LatheMachinBean bean : list) {
            selectBeans.add(new SelectBean(bean.getLatheName(), bean.getLatheCode()));
        }
        PopUtisl.showSelectPop(this, selectBeans, tvMachine, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                lathCode = bean.getId();
                tvMachine.setText(bean.getName());
            }
        }, "请选择机台");
    }

    @Override
    public void setProcessList(List<ProcessBean> list) {
        if (list.size() == 1){
            processBean=new SelectBean(list.get(0).getProcessName(),list.get(0).getFlowId());
            tvProcessId.setText(list.get(0).getProcessName());
            return;
        }
        List<SelectBean> selectBeans = new ArrayList<>();
        for (ProcessBean bean : list) {
            selectBeans.add(new SelectBean(bean.getProcessName(), bean.getFlowId()));
        }
        PopUtisl.showSelectPop(this, selectBeans, tvProcessId, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                processBean = bean;
                tvProcessId.setText(processBean.getName());
            }
        }, "请选择工序");
    }

    @OnClick({R.id.tv_processId, R.id.sb_scan, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_processId:
                presenter.getProcessByOrder(endFaceListBean.getProOrderNo(), endFaceListBean.getCenterCode());
                break;
            case R.id.sb_scan:
                if (TextUtils.isEmpty(tvProcessId.getText())) {
                    showMessage("工序不能为空");
                    return;
                }
               /* if (TextUtils.isEmpty(tvMachine.getText())) {
                    showMessage("机台不能为空");
                    return;
                }*/
                AffirmDialog affirmDialog = new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        ManMadeRequest request = new ManMadeRequest();
                        request.setFlowId(processBean.getId());
                       // request.setLatheCode(lathCode);
                        presenter.confirmArtificialDelivery(request);
                    }
                });
                affirmDialog.show("确定要出库吗？");
                break;
            case R.id.tv_move_out:
                startIntent(AppActivityKey.PICKINGACTIVITY);
                finishActivity();
                break;
        }
    }

    @Override
    public void setData(EndFaceDetailBean endFaceDetailBean) {
        tvConnectionCode.setText(endFaceDetailBean.getConnectionCode());
        tvStorageBin.setText(endFaceDetailBean.getWarehouseName());
        endFaceDetailAdapter.setNewInstance(endFaceDetailBean.getStocks());
    }


    @OnClick(R.id.tv_machine)
    public void onViewClicked() {
        presenter.getLatheListByCenter(endFaceListBean.getCenterCode());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.iv_close)
    public void onViewClicked1() {
        finishActivity();
    }

    @OnClick(R.id.tv_detail)
    public void onViewClicked2() {
        if (TextUtils.isEmpty(tvOrderNo.getText().toString())) {
            showMessage("请先扫生产订单码");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("order", tvOrderNo.getText().toString());
        startIntent(AppActivityKey.ORDERDETAILACTIVITY, bundle);
    }
}
